using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Inventory : MonoBehaviour, ISaveManager
{
    public static Inventory instance;

    #region  �������
    public List<ItemData_Weapon> startWeapons;                 //��ʼ����

    public List<InventoryItem> weaponInventory;        //������棨���ݣ�
    public Dictionary<ItemData_Weapon, InventoryItem> weaponDictionary;

    public List <ItemData_Weapon> weaponInEquipment;
    #endregion

    #region WeaponSoltUI(��������UI)
    [Header("Weapon UI")]
    [SerializeField] private Transform weaponSlotParent_I;    //�������۸��ڵ�(���)
    private Weapon_Slot_UI[] weaponSlots_I;                   //�������ۣ���棩
    [SerializeField] private Transform weaponSlotParent_E;    //�������۸��ڵ㣨װ����
    public Weapon_Slot_UI[] weaponSlots_E;                   //�������ۣ�װ����
    #endregion

    [Header("Data Base")]
    public List<InventoryItem> loadItems;
    public List<ItemData_Weapon> loadEquipments;

    private void Awake()
    {
        if (instance == null) instance = this;
        else Destroy(gameObject);
    }

    private void Start()
    {
        weaponInventory = new List<InventoryItem>();
        weaponDictionary = new Dictionary<ItemData_Weapon, InventoryItem>();

        weaponSlots_I = weaponSlotParent_I.GetComponentsInChildren<Weapon_Slot_UI>();       //��ʼ����������
        weaponSlots_E = weaponSlotParent_E.GetComponentsInChildren<Weapon_Slot_UI>(); 

        if(startWeapons != null)
            AddStartWeapon();

        if(loadEquipments.Count > 0)
        {
            foreach (ItemData_Weapon weapon in loadEquipments)
            {
                for(int i = 0; i < weaponSlots_E.Length; i++) 
                {
                    if (weaponSlots_E[i].emotionSoltType == weapon.emotionType)
                    {
                        weaponInEquipment.Add(weapon);

                        InventoryItem _weapon = new InventoryItem(weapon);
                        weaponSlots_E[i].UpdateWeaponSlot(_weapon);
                    }
                }
            }
            PlayerManager.instance.player.GetWeaponFromEquipment();
        }
    }

    /// ////////////////////////////////////////////////////////////////////////////////////////////////////
    #region Add Item Function(��ӵ��߹���)
    private void UpdateSlotUI()     //���¿���UI      
    {
        for (int i = 0; i < weaponInventory.Count; i++)
        {
            weaponSlots_I[i].UpdateWeaponSlot(weaponInventory[i]);
        }
    }

    public bool CanAddItem()                        //�ж��Ƿ����������Ʒ
    {                                                                                                       
        if (weaponInventory.Count > weaponSlots_I.Length)       
            return false;

        return true;
    }       

    public void AddStartWeapon()                    //��ӳ�ʼ����
    {
        for(int i = 0; i < startWeapons.Count; i++)
        {
            Debug.Log("OK3");
            AddItem(startWeapons[i]);
        }
    }

    public void AddItem(ItemData_Weapon _item)             //������ӵ��ߣ����ã�
    {
        if(_item.type == ItemType.Weapon) 
        {
            AddToWeaponInventory(_item);                ///************************************2
        }

        UpdateSlotUI();
    }

    public void AddWeaponToEquipment(InventoryItem _weapon)                 //��װ�������������ִ�У�
    {
        ItemData_Weapon weapon = _weapon.data as ItemData_Weapon;

        for(int i = 0; i < weaponSlots_E.Length; i++) 
        {
                Debug.Log("OK_Add load weapon to Equipment");
            if (weaponSlots_E[i].emotionSoltType == weapon.emotionType)
            {
                weapon.emotionToGet.Add(weapon.emotionType);
                weaponSlots_E[i].UpdateWeaponSlot(_weapon);
                weaponInEquipment.Add(weapon);
            }
        }
        PlayerManager.instance.player.GetWeaponFromEquipment();
    }

    public void RemoveWeaponFromEquipment(InventoryItem _weapon)        //��װ�����Ƴ�������ִ�У�
    {
        ItemData_Weapon weapon = _weapon.data as ItemData_Weapon;
        _weapon.data.hasBeenEquipment = false;
        weapon.emotionType = EmotionType.N;
        weapon.emotionToGet.Clear();

        for(int i = 0;i < weaponSlots_E.Length; i++)
        {
            if (weaponSlots_E[i].emotionSoltType == GlobalVariables.instance.emotionSoltToSwitch)
            {
                weaponSlots_E[i].UpdateWeaponSlot(null);
                weaponInEquipment.Remove(weapon);
            }
        }
        GlobalVariables.instance.emotionSoltToSwitch = EmotionType.N;
        PlayerManager.instance.player.GetWeaponFromEquipment();
    }

    public void SwitchWeaponWithInventory(InventoryItem _weapon)        //�ÿ����������װ����������ִ�У�
    {
        for(int i = 0; i < weaponInventory.Count; i++) 
        {
            if (weaponInventory[i] == GlobalVariables.instance.weaponToSwitch) 
            {
                ItemData_Weapon weapon = weaponInventory[i].data as ItemData_Weapon;
                weaponInventory[i].data.hasBeenEquipment = false;
                weapon.emotionType = EmotionType.N;
                weapon.emotionToGet.Clear();
                weaponInEquipment.Remove(weapon);
            }
        }
        AddWeaponToEquipment(_weapon);
        PlayerManager.instance.player.GetWeaponFromEquipment();
    }


    public void AddToWeaponInventory(ItemData_Weapon _item)        //�������������ִ�У�
    {
        if (!weaponDictionary.ContainsKey(_item))                   ///************************************1
        {
            InventoryItem newItem = new InventoryItem(_item);
            weaponInventory.Add(newItem);
            weaponDictionary.Add(_item, newItem);
        }
        else
            return;
    }

    #endregion

    /// /////////////////////////////////////////////////////////////////////////////////////
    #region Save && Load������ͼ������ݣ�

    public void LoadData(GameData _data)
    {
        //���ؿ������
        LoadInventoryData(_data);                       ///************************************4

        LoadEquipmentData(_data);
    }

    private void LoadInventoryData(GameData _data)          //���ؿ������
    {
        foreach (KeyValuePair<string, int> pair in _data.inventory)
        {
            foreach (var weapon in GetDataBase())
            {
                if (weapon != null && weapon.itemID == pair.Key)
                {
                    InventoryItem itemToLoad = new InventoryItem(weapon);
                    itemToLoad.stackSize = pair.Value;

                    loadItems.Add(itemToLoad);
                }
            }
        }

        //if (loadItems.Count > 0)
        //{
        //    foreach (InventoryItem item in loadItems)
        //    {
        //        for (int i = 0; i < item.stackSize; i++)
        //        {
        //            AddItem(item.data as ItemData_Weapon);              ///************************************3
        //        }
        //    }
        //}
    }

    private void LoadEquipmentData(GameData _data)      //����װ������
    {
        foreach (string data in _data.Equipment)
        {
            foreach (var weapon in GetDataBase())
            {
                if (weapon != null && weapon.itemID == data)
                {
                    InventoryItem itemToload = new InventoryItem(weapon);

                    loadEquipments.Add(itemToload.data as ItemData_Weapon);
                }
            }
        }

        //if (loadEquipments.Count > 0)
        //{
        //    foreach (ItemData_Weapon weapon in loadEquipments)
        //    {
        //    Debug.Log("OK");
        //        for(int i = 0; i < weaponSlots_E.Length; i++) 
        //        {
        //            if (weaponSlots_E[i].emotionSoltType == weapon.emotionType)
        //            {
        //                weaponInEquipment.Add(weapon);

        //                InventoryItem _weapon = new InventoryItem(weapon);
        //                weaponSlots_E[i].UpdateWeaponSlot(_weapon);
        //            }
        //        }
        //    }
        //}
    }

    public void SaveData(ref GameData _data)
    {
        SaveInventoryData(_data);


        SaveEquipmentData(_data);

        //foreach (KeyValuePair<ItemData_Weapon, InventoryItem> pair in itemDictionary)
        //{
        //    _data.inventory.Add(pair.Key.itemID, pair.Value.stackSize);
        //}
    }

    private void SaveEquipmentData(GameData _data)          //����װ������
    {
        _data.Equipment.Clear();

        foreach (ItemData_Weapon weapon in weaponInEquipment)
        {
            _data.Equipment.Add(weapon.itemID);
        }
    }

    private void SaveInventoryData(GameData _data)      //����������
    {
        _data.inventory.Clear();

        foreach (KeyValuePair<ItemData_Weapon, InventoryItem> pair in weaponDictionary)
        {
            _data.inventory.Add(pair.Key.itemID, pair.Value.stackSize);
        }
    }

    private List<ItemData> GetDataBase()
    {
        List<ItemData> weaponDataBase = new List<ItemData>();
        string[] assetNames = AssetDatabase.FindAssets("", new[] { "Assets/Object/Weapons" });

        foreach (string SOName in assetNames)
        {
            var SOpath = AssetDatabase.GUIDToAssetPath(SOName);
            var weaponData = AssetDatabase.LoadAssetAtPath<ItemData_Weapon>(SOpath);
            weaponDataBase.Add(weaponData);
        }

        return weaponDataBase;
    }
    #endregion
}
